package com.xzx.nio;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 *
 * @author xinzhixuan
 * @version 1.0
 * @date 2025/9/23 11:49
 */
public class ChannelTest {

    /**
     * 写字符串到文件中
     */
    @Test
    public void testWrite2Channel() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("test.txt");
        FileChannel channel = fileOutputStream.getChannel();
        ByteBuffer buffer = ByteBuffer.wrap("hello world".getBytes());
        System.out.println("position=" + buffer.position() + " limit=" + buffer.limit() + " capacity=" + buffer.capacity());
        channel.write(buffer);
        channel.close();
        fileOutputStream.close();
    }

    /**
     * 从文件中读取内容
     */
    @Test
    public void testReadFromChannel() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("test.txt");
        FileChannel channel = fileInputStream.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(3);
        int len;
        while ((len = channel.read(buffer)) > 0) {
            System.out.println("position=" + buffer.position() + " limit=" + buffer.limit() + " capacity=" + buffer.capacity());
            buffer.flip();
            System.out.println("读取到的数据：" + new String(buffer.array(), 0, len));
            System.out.println("position=" + buffer.position() + " limit=" + buffer.limit() + " capacity=" + buffer.capacity());
            buffer.clear();
            System.out.println("===========================");
        }
    }

    @Test
    public void testCopyFile() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("test.txt");
        FileChannel readChannel = fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("test2.txt");
        FileChannel writeChannel = fileOutputStream.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (readChannel.read(buffer) != -1) {
            buffer.flip();
            writeChannel.write(buffer);
            buffer.clear();
        }
        writeChannel.close();
        readChannel.close();
        fileOutputStream.close();
        fileInputStream.close();
    }

    /**
     * 使用transferFrom方法
     * 底层使用的0拷贝
     */
    @Test
    public void testTransferFrom() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("test.txt");
        FileChannel readChannel = fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("test2.txt");
        FileChannel writeChannel = fileOutputStream.getChannel();
        writeChannel.transferFrom(readChannel, 0, readChannel.size());
        writeChannel.close();
        readChannel.close();
        fileOutputStream.close();
        fileInputStream.close();
    }

    /**
     * 使用transferTo方法
     * 底层使用的0拷贝
     */
    @Test
    public void testTransferTo() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("test.txt");
        FileChannel readChannel = fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("test2.txt");
        FileChannel writeChannel = fileOutputStream.getChannel();
        readChannel.transferTo(0, readChannel.size(), writeChannel);
        writeChannel.close();
        readChannel.close();
        fileOutputStream.close();
        fileInputStream.close();
    }
}
